<?php

namespace common\logics\shop\bak;

use Yii;
use common\helpers\ArrayHelper;

/**
 * 商品规格信息模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class SpuSpec_1 extends \common\models\views\ViewSkuSpec {

    /**
     * {@inheritdoc}
     */
    public static function findById($id, $specs) {
        $skus = static::find()->where([
            'product_id' => $id
        ])->all();

        $checkSpecValues = $specs ? explode(',', $specs) : [];
//        $checkSpecIds = explode(',', $specids);
        return static::formatSpecsGroup($skus, $checkSpecValues);
//        return static::formatSpecsGroup($skus, $checkSpecValues, $checkSpecIds);
    }

    /**
     * 分组格式化规格属性
     * return array
     */
    public static function formatSpecsGroup($specs, $checkSpecValues = []) {
        if (empty($specs)) {
            return [];
        }


        $keys = $items = [];
        $goods = [];

        $tempId = [];

        //所有有效的库存sku
        $skuSpecs = [];
        foreach ($specs as $spec) {
            if ($spec->allowSales > 0) {
                $skuSpecs[$spec->goods_id][] = $spec->spec_value_id;
            }
            if (in_array($spec->spec_value_id, $checkSpecValues)) {
                $tempId[$spec->spec_id] = 1;
            }
        }

        //选择的规格对应的所有规格值
        $goodsSpecIds = [];

        $forceSku = null;

        $newSpecs = [];
        //检测是否定位到SKU
        foreach ($skuSpecs as $skuid => $skuspec) {
            if (empty(array_diff($skuspec, $checkSpecValues))) {
                $forceSku = $skuspec;
                break;
            }
            if ($checkSpecValues == array_intersect($checkSpecValues, $skuspec)) {
                $goodsSpecIds = ArrayHelper::merge($goodsSpecIds, $skuspec);
                $newSpecs[$skuid] = $skuspec;
            }
        }

        //唯一确定了SKU的情况
        if ($forceSku) {
            $count = count($forceSku);
            $queueSpecs = array_merge($forceSku, $forceSku);
            $composeSpecs = [];
            $goodsSpecIds = [];
            for ($i = 0; $i < $count; $i++) {
                $tempCheckSpecValues = array_slice($queueSpecs, $i, $count - 1);
                //检测是否定位到SKU
                foreach ($skuSpecs as $skuid => $skuspec) {
                    if ($tempCheckSpecValues == array_intersect($tempCheckSpecValues, $skuspec)) {
                        $goodsSpecIds = ArrayHelper::merge($goodsSpecIds, $skuspec);
                        $composeSpecs[$skuid] = $skuspec;
                    }
                }
            }
            $newSpecs = $composeSpecs;
        }
        $goodsIds = array_keys($newSpecs);
        $canchecked = [];
        foreach ($specs as $spec) {
            if ($forceSku) {
                $goods = $spec;
            }
            $_goods = ArrayHelper::getValue($items, [$spec->spec_id, $spec->spec_value_id, 'goods'], []);
            if ($spec->allowSales > 0) {
                if (in_array($spec->goods_id, $goodsIds)) {
                    $_goods[$spec->goods_id][] = $spec->goods_id;
                } elseif (in_array($spec->spec_value_id, $goodsSpecIds)) {
                    $_goods[$spec->goods_id] = 1;
                } elseif (in_array($spec->spec_id, $tempId)) {
                    $_goods[$spec->goods_id] = 2;
                }
            }
            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'clickable' => true,
                'goods' => $_goods,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }

        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }

            $_kvitems = [];
            foreach ($_items as $_iv) {
                $_skus = ArrayHelper::remove($_iv, 'goods', []);
                $_iv['clickable'] = count($_skus) > 0 ? true : false;
                if ($_iv['checked'] && !$_iv['clickable']) {
                    $_iv['checked'] = false;
                }
                $_kvitems[] = $_iv;
            }
            $kvls['items'] = $_kvitems;
            $data[] = $kvls;
        }

        return [
            'checkSpecValues' => $checkSpecValues,
            'goods' => $goods,
            'specs' => $data,
        ];
    }

    /**
     * 分组格式化规格属性
     * return array
     */
    public static function formatSpecsGroup3331($specs, $checkSpecValues = []) {
        if (empty($specs)) {
            return [];
        }
        $keys = $items = [];
        $goods = [];
        $skuSpecs = [];
        $checkSpecIds = [];
        foreach ($specs as $spec) {
            $skuSpecs[$spec->goods_id]['specs'][] = $spec->spec_value_id;
            $skuSpecs[$spec->goods_id]['stock'] = $spec->allowSales;
            if (in_array($spec->spec_value_id, $checkSpecValues)) {
                $checkSpecIds[$spec->spec_id] = 1;
            }
            if ($spec->allowSales > 0) {
                $goods[$spec->goods_id] = [
                    'id' => $spec->spec_value_id,
                    'name' => $spec->spec_value,
                ];
            }
        }

        $skuId = 0;
        foreach ($skuSpecs as $goods_id => $skuspec) {
            if (empty(array_diff($skuspec['specs'], $checkSpecValues))) {
                $skuId = $goods_id;
                break;
            }
        }

        $sku = null;
        foreach ($specs as $spec) {
            if (empty($sku) && $skuId) {
                $sku = $spec;
            }
            $_goods = ArrayHelper::getValue($items, [$spec->spec_id, $spec->spec_value_id, 'goods'], []);

//            if (empty($checkSpecValues)) {
//                $_goods[$spec->goods_id] = $skuSpecs[$spec->goods_id]['specs'];
//            } else if ($checkSpecValues == array_intersect($checkSpecValues, $skuSpecs[$spec->goods_id]['specs'])) {
//                $_goods[$spec->goods_id] = $skuSpecs[$spec->goods_id]['specs'];
//            } elseif ($skuId) {
//                $tempCheckValues = [];
//                foreach ($checkSpecValues as $checkvid) {
//                    if (in_array($checkvid, $skuSpecs[$spec->goods_id]['specs'])) {
//                        $_goods[$spec->goods_id] = $skuSpecs[$spec->goods_id]['specs'];
//                        break;
//                    }
//                }
//            } else {
//                if (isset($checkSpecIds[$spec->spec_id])) {
//                    $_goods[$spec->goods_id] = $skuSpecs[$spec->goods_id]['specs'];
//                }
//            }

            if (empty($checkSpecValues)) {
                $_goods[$spec->goods_id] = $skuSpecs[$spec->goods_id]['specs'];
            } else {
                $intersect = array_intersect($checkSpecValues, $skuSpecs[$spec->goods_id]['specs']);
                if (!empty($intersect) && $skuSpecs[$spec->goods_id]['stock'] > 0) {
                    $_goods[$spec->goods_id] = $skuSpecs[$spec->goods_id]['specs'];
                }
            }

            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'optional' => true,
                'goods' => $_goods,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }

        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }
            $kvls['items'] = array_values($_items);
            $data[] = $kvls;
        }

        return [
//            'gslist' => $skuSpecs,
            'sku' => $sku,
            'specs' => $data,
        ];
    }

    /**
     * 分组格式化规格属性
     * return array
     */
    public static function formatSpecsGroup333($specs, $checkSpecValues = []) {
        if (empty($specs)) {
            return [];
        }
        $keys = $items = $goods = [];

        $sku = null;
        foreach ($specs as $spec) {
            $_goods = ArrayHelper::getValue($items, [$spec->spec_id, $spec->spec_value_id, 'goods'], []);
            if ($spec->allowSales > 0) {
                $_goods[$spec->goods_id] = $spec;
            }
            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'optional' => true,
                'goods' => $_goods,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }

        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }
            $kvls['items'] = array_values($_items);
            $data[] = $kvls;
        }

        return [
//            'gslist' => $skuSpecs,
            'sku' => $sku,
            'specs' => $data,
        ];
    }

    /**
     * 分组格式化规格属性
     * return array
     */
    public static function formatLower($specs, $checkSpecValues = []) {
        if (empty($specs)) {
            return [];
        }

        return ArrayHelper::getValue($goods, [$spec->goods_id, 'stock'], 0);
        $keys = $items = $goods = [];

        $failsgoods = [];

        foreach ($specs as $spec) {
            $_specvls = ArrayHelper::getValue($goods, [$spec->goods_id, 'stock'], 0);
            $_specvls[] = $spec->spec_value_id;
            $goods[$spec->goods_id] = [
                'goods_id' => $spec->goods_id,
                'stock' => $spec->allowSales,
                'name' => $spec->name,
                'specs' => $_specvls,
            ];
            if ($spec->allowSales <= 0) {
                $failsgoods[$spec->goods_id] = $_specvls;
            }

            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'optional' => $spec->allowSales > 0,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }


        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }
            $specvalueids = array_keys($_items);

            if (empty(array_intersect($specvalueids, $checkSpecValues))) {
                $merges = ArrayHelper::merge($checkSpecValues, $specvalueids);
                foreach ($failsgoods as $fails) {
                    if ($fails == array_intersect($fails, $merges)) {
                        foreach ($fails as $fid) {
                            if (in_array($fid, $specvalueids)) {
                                $_items[$fid]['optional'] = false;
                            }
                        }
                    }
                }
            }

            $kvls['items'] = array_values($_items);
            $data[] = $kvls;
        }


        return [
//            'gslist' => $gslist,
//            'goods' => $goods,
            'specs' => $data,
        ];
    }

    /**
     * 分组格式化规格属性
     * return array
     */
    public static function formatSpecsGroup2($specs, $checkSpecValues = []) {
        if (empty($specs)) {
            return [];
        }
        $keys = $items = $goods = [];

        $failsgoods = [];
        foreach ($specs as $spec) {
            $_specvls = ArrayHelper::getValue($goods, [$spec->goods_id, 'specs'], []);
            $_specvls[] = $spec->spec_value_id;
            $goods[$spec->goods_id] = [
                'goods_id' => $spec->goods_id,
                'stock' => $spec->allowSales,
                'name' => $spec->name,
                'specs' => $_specvls,
            ];
            if ($spec->allowSales <= 0) {
                $failsgoods[$spec->goods_id] = $_specvls;
            }

            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'optional' => $spec->allowSales > 0,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }


        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }
            $specvalueids = array_keys($_items);

            if (empty(array_intersect($specvalueids, $checkSpecValues))) {
                $merges = ArrayHelper::merge($checkSpecValues, $specvalueids);
                foreach ($failsgoods as $fails) {
                    if ($fails == array_intersect($fails, $merges)) {
                        foreach ($fails as $fid) {
                            if (in_array($fid, $specvalueids)) {
                                $_items[$fid]['optional'] = false;
                            }
                        }
                    }
                }
            }

            $kvls['items'] = array_values($_items);
            $data[] = $kvls;
        }


        return [
//            'gslist' => $gslist,
//            'goods' => $goods,
            'specs' => $data,
        ];
    }

}
